package servlets;

import java.io.IOException;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

import javax.jdo.JDOObjectNotFoundException;
import javax.jdo.PersistenceManager;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;

import dataTypes.*;

/*
 * A business adds new deal coupon
 */

@SuppressWarnings("serial")
public class addNewCoupon extends HttpServlet {


	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
	

			response.setContentType("text/html"); 

			int sTimeH = Integer.parseInt(request.getParameter("startH").substring(0, 2));
	     	int eTimeH = Integer.parseInt(request.getParameter("endH").substring(0, 2));
	     	
	     	int sTimeM = Integer.parseInt(request.getParameter("startH").substring(3));
	     	int eTimeM = Integer.parseInt(request.getParameter("endH").substring(3));

	     	

	     	int num = Integer.parseInt(request.getParameter("num"));
			int max = Integer.parseInt(request.getParameter("max"));
			int type = Integer.parseInt(request.getParameter("type"));
			int full = Integer.parseInt(request.getParameter("full"));
			int percent = Integer.parseInt(request.getParameter("percent"));
			
			int bId=Integer.parseInt(request.getParameter("bId"));
			
			boolean b=appManager.getInstance().isBusiness(bId);
			
			if (!b){
				response.sendRedirect("/adminlogin.html");
				return;
			}
			
			
			boolean ret=appManager.getInstance().addCoupon(bId,num,max,type,full,percent,sTimeH,sTimeM,eTimeH,eTimeM);
			
			if (!ret){
				response.sendRedirect("/admin.html");
				return;
			}
			//sends Push to all users - filtering should be done within C2DMSendMessage
			C2DMSendMessage.sendMessageToAll();
			
			response.sendRedirect("/adminchose.html?bId="+bId);
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}